﻿using FarmSmartSleep.Common.Domain.RBACFile;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace R.FarmSmartSleep.RBAC.API.Apliction.Command
{
    public class EmployeeDetailHander : IRequestHandler<EmployeeDetailCommand, ResultT<Employee>>
    {

        private readonly IBaseRepository<Employee> employeeRepository;
        private readonly ILogger<EmployeeDetailHander> logger;

        public EmployeeDetailHander(IBaseRepository<Employee> employeeRepository, ILogger<EmployeeDetailHander> logger)
        {
            this.employeeRepository = employeeRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 获取当前登录人信息
        /// </summary>
        /// <param name="request">获取当前登录人信息请求对象</param>
        /// <param name="cancellationToken"></param>
        /// <returns>获取当前登录人信息响应对象</returns>
        public async Task<ResultT<Employee>> Handle(EmployeeDetailCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new ResultT<Employee>();
                var info = employeeRepository.GetAll().FirstOrDefault(c => c.EmployeeId == request.EmployeeId);
                if (info != null) 
                {
                    result.Item = info;
                }
                return result;
            }
            catch (Exception ex )
            {
                logger.LogError("获取人员信息方法调用出错" + ex);
                throw;
            }
        }
    }
}
